/*
 * LinkSet.java
 *
 * Created on June 19, 2007, 9:51 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster;

import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.util.HashSet;
import java.util.Iterator;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;
import org.fpdev.apps.rtemaster.gui.map.MapDrawable;

/**
 *
 * @author demory
 */
public class LinkSet extends HashSet<BLink> implements MapDrawable {

  private Color color_;

  /** Creates a new instance of LinkSet */
  public LinkSet(Color color) {
    super();
    color_ = color;
  }

  public int connectingLinks(BNode node) {
    Iterator<BLink> links = this.iterator();
    int count = 0;
    while (links.hasNext()) {
      BLink link = links.next();
      if (link.getFNode() == node || link.getTNode() == node) {
        count++;
      }
    }
    return count;
  }

  public void draw(MapCanvas canvas) {
    Iterator<BLink> links = iterator();
    while (links.hasNext()) {
      canvas.setLineWidth(3);
      canvas.setColor(color_);
      BLink link = links.next();
      link.drawCurve(canvas);
    //System.out.println("drew link "+link.getID());
    }
  //System.out.println("drew "+this.size()+ " links");
  }

  public Rectangle2D getBoundingBox() {
    if (isEmpty()) {
      return null;
    }

    Iterator<BLink> links = iterator();
    Rectangle2D rect = links.next().getBoundingBox();
    while (links.hasNext()) {
      BLink link = links.next();
      if (link != null) {
        rect = rect.createUnion(link.getBoundingBox());
      }
    }
    return rect;
  }
}
